/**
 * Author: ligj@mapbar.com
 * Create Time: 2018/01/11 13:15
 * Description:
 */

import Vue from 'vue'
import 'babel-polyfill'
import moment from 'moment'
import initStore from './store'
import initRouter from './utils/router'
import { setEnableRole } from './utils/roles'

import http from './utils/request';

import ElementUI from 'element-ui'
import events from './utils/events'
// import timeout from './utils/timeout'
// import roles from './utils/roles'
import ECharts from 'vue-echarts/components/ECharts.vue'
import WRouterView from './components/w-router-view'
import MMap from './components/m-map'
// import NMap from './components/nglp-map'
import WSidebar, { setSidebarMenu } from './components/sidebar';
import EventEmitter from 'events';

let crypto = require('crypto');

import 'echarts'
Vue.filter('moment', function (value, formatString) {
    formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
    if(!value || isNaN(value)) return '';
    value = value*1;
    return moment(value).format(formatString); // 这是时间戳转时间
});

// 列表数据返回空字符串 或 null 页面显示 - 或者自定义符号
Vue.prototype.addLine = function (data,str) {
    str = str || "--";
    if(data && data.length > 0 ){
        data.map(item => {
            for( let key in item) {
                if(item[key] === "" || item[key]=== null || item[key] === undefined){
                    item[key] = str;
                }
            }
        });
    }
    return data;
};
// 加密
Vue.prototype.md5 = function (str) {
    let md5Sum = crypto.createHash('md5');
    md5Sum.update(str);
    str = md5Sum.digest('hex');
    return str;
};

Vue.mixin({
    methods: {
        $filters(c, d){
            return d.filter((i) => {
                return Object.keys(c).every(k =>{
                    return String(i[k]).toLowerCase().includes(String(c[k]).trim().toLowerCase());
                })
            })
        },
        $translate(str) {
            if (typeof str !== 'string') {
                str += '';
            }
            return str.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
        }
    }
})

Vue.use(ElementUI, {
    size: 'small'
});
//注册echart组件，demo https://github.com/Justineo/vue-echarts/blob/master/demo/Demo.vue
Vue.component('e-charts', ECharts);
Vue.component('w-router-view', WRouterView);
Vue.component('m-map', MMap);
// Vue.component('n-map', NMap);
Vue.component('w-sidebar', WSidebar);

Vue.config.productionTip = false;

Vue.eventEmitter = new EventEmitter();


import './style/core.scss';

function VueUse(...arg) {
	Vue.use(...arg);
}

export {
    initRouter,
    initStore,
    http,
	VueUse,
	setEnableRole,
    setSidebarMenu
}

export default function ({el, store, routes, App},enableRole = false) {
	Vue.prototype.$enableRole = enableRole;
    return new Vue({
        el,
        router: initRouter(routes),
        store: initStore(store),
        components: {App},
        template: '<App/>'
    })
}
